<?php
require_once(dirname(__FILE__) . '/app.php');

if (!$teams) { 
include template('open_city');
	die();
 }

$signPackage = signpackage();
 
$now = time();
$detail = array();

foreach($teams AS $index => $team) {

	if($team['end_time']<$team['begin_time']){$team['end_time']=$team['begin_time'];}
	$diff_time = $left_time = $team['end_time']-$now;
	if ( $team['team_type'] == 'seconds' && $team['begin_time'] >= $now ) {
		$diff_time = $left_time = $team['begin_time']-$now;
	}

	/* progress bar size */
	$detail[$team['id']]['bar_size'] = ceil(190*($team['now_number']/$team['min_number']));
	$detail[$team['id']]['bar_offset'] = ceil(5*($team['now_number']/$team['min_number']));

	$left_day = floor($diff_time/86400);
	$left_time = $left_time % 86400;
	$left_hour = floor($left_time/3600);
	$left_time = $left_time % 3600;
	$left_minute = floor($left_time/60);
	$left_time = $left_time % 60;

	$detail[$team['id']]['diff_time'] = $diff_time;
	$detail[$team['id']]['left_day'] = $left_day;
	$detail[$team['id']]['left_hour'] = $left_hour;
	$detail[$team['id']]['left_minute'] = $left_minute;
	$detail[$team['id']]['left_time'] = $left_time;
	$detail[$team['id']]['is_today'] = $team['begin_time'] + 3600*24 > time() ? 1:0;

	/* state */
	$team['state'] = team_state($team);
	$teams[$index] = $team;
}
$team = null;



$daytimenow_ev56_s = time();
/*判断预告，过期，秒杀，热销*/
$condition_ev56_s = array(
	'team_type' => 'normal',
	'max_number >0',
	"begin_time <= $daytimenow_ev56_s ",
	"end_time > $daytimenow_ev56_s",
);

$city_id = abs(intval($city['id']));
/*echo $city_id; 判断城市*/
$condition_ev56_s[] = "((city_ids like '%@$city_id@%' or city_ids like '%@0@%') or city_id in(0,$city_id))";

$group_id = abs(intval($_GET['gid']));
/*大分类ID*/
if ($group_id){$condition_ev56_s['group_id'] = $group_id;}

function current_teamcategory_ev56($gid='0') {
    global $city;
    $city_id = $city['id'];
	$today = strtotime(date('Y-m-d'));
	$condition_ev56_s_c = array(
		'team_type' => 'normal',
		"begin_time <= '$today'",
		"end_time > '$today'",
	);
    $condition_ev56_s_c[] = "(city_ids like '%@$city_id@%' or city_ids like '%@0@%') or (city_ids = '' and city_id in(0,$city_id))";
	$categorys_ev56_s_c = DB::LimitQuery('category', array(
		'condition' => array( 'zone' => 'group','fid' => '0','display' => 'Y' ),
		'order' => 'ORDER BY sort_order DESC, id DESC',
	));
	$categorys_ev56_s_c = Utility::OptionArray($categorys_ev56_s_c, 'id', 'name');
	$a = array();
	foreach($categorys_ev56_s_c AS $id=>$name) {
	    $condition_ev56_s_c['group_id'] = $id;
	    $num= Table::Count('team', $condition_ev56_s_c);
		$a["/index.php?gid=$id"] = $name.'<span>('.$num.')</span>';
	}	
	$l = "/index.php?gid=$gid";
	if (!$gid) $l = "/index.php";
	return current_link_ev56($l, $a, true);
}
function current_link_ev56($link, $links, $span=false) {
	$html = '';
	$span = $span ? '<span></span>' : '';
	foreach($links AS $l=>$n) {
		if (trim($l,'/')==trim($link,'/')) {
			$html .= "<a href=\"$l\"  class=\"select\" >$n</a>";
		}
		else $html .= "<a href=\"$l\">$n</a>";
	}
	return $html;
}

/*小分类*/
$ev56_category_fl_texts = DB::LimitQuery('category', array(
	'condition' => array(
		'zone' => 'group',
		'display' => 'y',
		'fid<>0',
),
	'order' => 'ORDER BY sort_order DESC',
));
$ev56_category_fl_texts_2 = DB::LimitQuery('category', array(
	'condition' => array(
		'zone' => 'group',
		'display' => 'y',
		'fid' => $group_id,
),
	'order' => 'ORDER BY sort_order DESC',
));

$fid_s = abs(intval($_GET['fid_s']));
/*echo $fid_s; 判断分类ID*/
if ($group_id<>0){$condition_ev56_s['group_id'] = $group_id;}
if ($fid_s<>0){$condition_ev56_s['sub_id'] = $fid_s;}

if(!empty($_GET['type']))
{
if($_GET['show']=='all')$pagesize=100;
else
$pagesize=$index_ev56_ns;
}
if($count>$index_ev56_ns)$show=1;

if($_GET['show']=='all'){
$show=1;$p=0;
}
if($_GET['s']=="n")
{
$order='ORDER BY begin_time DESC, id DESC';
}
elseif($_GET['s']=="b")
{
$order='ORDER BY now_number DESC, id DESC';
}
elseif($_GET['s']=="jg")
{
$order='ORDER BY team_price ASC, id DESC';
}
elseif($_GET['s']=="jgd")
{
$order='ORDER BY team_price DESC, id DESC';
}
elseif($_GET['s']=="jg_50")
{
$condition_ev56_s []= "( team_price <= 50 )";
$order='ORDER BY  sort_order DESC,begin_time DESC, id DESC';
}
elseif($_GET['s']=="jg_50_100")
{
$condition_ev56_s []= "( team_price > 50 and team_price<=100 )";
$order='ORDER BY  sort_order DESC,begin_time DESC, id DESC';
}
elseif($_GET['s']=="jg_100_300")
{
$condition_ev56_s []= "( team_price > 100 and team_price<=300 )";
$order='ORDER BY  sort_order DESC,begin_time DESC, id DESC';
}
elseif($_GET['s']=="jg_300")
{
$condition_ev56_s []= "( team_price > 300 )";
$order='ORDER BY  sort_order DESC,begin_time DESC, id DESC';
}
elseif($_GET['s']=="zk")
{
$order='ORDER BY sort_order DESC, market_price - team_price ASC, id DESC';
}
elseif($_GET['s']=="zkd")
{
$order='ORDER BY  sort_order DESC, market_price - team_price DESC, id DESC';
}
elseif($_GET['fid_s'])
{
$condition_ev56_s []= "( sub_id  =  $fid_s )";
$order='ORDER BY  sort_order DESC,begin_time DESC, id DESC';
}
else 
{
$order='ORDER BY  sort_order DESC,begin_time DESC, id DESC';
}
		

$p=1;
$index_ev56_size = abs(intval($INI['system']['indexteam']));
//$index_ev56_size = 2;

$count = Table::Count('team', $condition_ev56_s);
if($_GET[page]<2){
if($count>=$index_ev56_size){$current_count=$index_ev56_size;}
else{
 $current_count=$count;}
}
else
$current_count=$count-($_GET[page]-1)*$index_ev56_size;

list($pagesize, $offset, $pagestring) = pagestring($count, $index_ev56_size);

$teams_ev56_sql = DB::LimitQuery('team', array(
	'condition' => $condition_ev56_s,
	'order' => $order,
	'size' => $index_ev56_size,
	'offset' => $offset,
));

foreach($teams_ev56_sql AS $index => $team) {

	if($team['end_time']<$team['begin_time']){$team['end_time']=$team['begin_time'];}
	$diff_time = $left_time = $team['end_time']-$now;
	if ( $team['team_type'] == 'seconds' && $team['begin_time'] >= $now ) {
		$diff_time = $left_time = $team['begin_time']-$now;
	}

	/* progress bar size */
	$detail[$team['id']]['bar_size'] = ceil(190*($team['now_number']/$team['min_number']));
	$detail[$team['id']]['bar_offset'] = ceil(5*($team['now_number']/$team['min_number']));

	$left_day = floor($diff_time/86400);
	$left_time = $left_time % 86400;
	$left_hour = floor($left_time/3600);
	$left_time = $left_time % 3600;
	$left_minute = floor($left_time/60);
	$left_time = $left_time % 60;

	$detail[$team['id']]['diff_time'] = $diff_time;
	$detail[$team['id']]['left_day'] = $left_day;
	$detail[$team['id']]['left_hour'] = $left_hour;
	$detail[$team['id']]['left_minute'] = $left_minute;
	$detail[$team['id']]['left_time'] = $left_time;
	$detail[$team['id']]['is_today'] = $team['begin_time'] + 3600*24 > time() ? 1:0;

	/* state */
	$team['state'] = team_state($team);
	$teams_ev56_sql[$index] = $team;
}


if(empty($_GET[gid])) $indexs=1;
$gid=$_GET[gid]?$_GET[gid]:0;
$fid_s=$_GET[fid_s]?$_GET[fid_s]:0;

if(isset($INI['system']['secsite']) && !empty($INI['system']['secsite']))
	$cur_url = $INI['system']['wwwprefix'].$INI['system']['secsite'].'index.php?gid='.$gid;
else 
	$cur_url = $INI['system']['wwwprefix'].'/index.php?gid='.$gid;

if(isset($_GET['page'])  && intval($_GET['page']) > 1)
{
	$s_html = '';
	$result = array('ret'=>1,'offset'=>$offset);
	if(!empty($teams_ev56_sql)){
		$result['ret'] = 0;
	}
	foreach($teams_ev56_sql as $tindex =>$oneev56)
	{
		$s_html .= '		<div class="tuan_g">';
		if(isset($INI['system']['secsite']) && !empty($INI['system']['secsite']))
			$s_html .= '			<a href="'.$INI['system']['wwwprefix'].$INI['system']['secsite'].'team.php?id='.$oneev56['id'].'">';
		else 
			$s_html .= '			<a href="'.$INI['system']['wwwprefix'].'team.php?id='.$oneev56['id'].'">';
	
		$s_html .= '				<div class="tuan_g_img">';
		$s_html .= '					<img src="'.team_image($oneev56['image']).'">';
		$s_html .= '					<span class="tuan_mark tuan_mark2">';
		$s_html .= '					<b>'.team_discount($oneev56).'折</b>';
		$s_html .= '					<span>'.$oneev56['min_number'].'人团</span>';
		$s_html .= '					</span>';
		$s_html .= '				</div>';
		$s_html .= '				<div class="tuan_g_info">';
		$s_html .= '					<p class="tuan_g_name">'.$oneev56['title'].'</p>';
		$s_html .= '					<p class="tuan_g_cx">'.$oneev56['summary'].'</p>';
		$s_html .= '				</div>';
		$s_html .= '				<div class="tuan_g_core">';
		$s_html .= '					<div class="tuan_g_price">';
		$s_html .= '						<span>'.$oneev56['min_number'].'人团</span>';
		$s_html .= '						<b>&yen;'.moneyit($oneev56['pin_price']).'</b>';
		$s_html .= '					</div>';
		$s_html .= '					<div class="tuan_g_btn">去开团</div>';
		$s_html .= '				</div>';
		$s_html .= '				<div class="tuan_g_mprice">市场价：';
		$s_html .= '					<del>&yen;'.moneyit($oneev56['market_price']).'</del>';
		$s_html .= '				</div>';
		$s_html .= '			</a>';
		$s_html .= '		</div>';
		
		
	}
	
	$result['html'] = $s_html;
	echo json_encode($result);
	die();
}

$news = DB::LimitQuery('news', array(
	
	'order' => 'ORDER BY id asc',
	
));
//image
//var_dump($news);die();
/*一级分类*/
$catelist = DB::LimitQuery('category', array(
	'condition' => array(
		'zone' => 'group',
		'display' => 'y',
		'fid=0',
),
	'order' => 'ORDER BY sort_order DESC',
));


$city_id = abs(intval($city['id']));
/*echo $city_id; 判断城市*/
$condition_ev56_s[] = "";
$ob_where = " where t.team_type='normal' and t.max_number> 0 and  t.begin_time < ".$daytimenow_ev56_s.' and end_time > '.$daytimenow_ev56_s;
$ob_where .= " and ((t.city_ids like '%@$city_id@%' or t.city_ids like '%@0@%') or t.city_id in(0,$city_id)) ";

$order_sql ="SELECT  DISTINCT `name`,ca.*  FROM  team as t  LEFT JOIN category  as ca ON ca.id=t.group_id ".$ob_where;

$catelist = DB::GetQueryResult($order_sql,false);

if($INI['option']['indexmultimeituan'] == 'Y'){

	include template('team_meituan');
}else {
	include template('team_multi');
};
